package com.example.hardwork2;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

public class Card extends FrameLayout {
    private TextView label;  //呈现的文字
    private int num = 0;     //绑定的编号
    // 设置背景色
    private int defaultBackColor = Color.argb(70,255,255,255);

    public Card(@NonNull Context context) {
        super(context);
        label = new TextView(getContext());   //显示下
        label.setTextSize(38);
        label.setTextColor(Color.argb(230,136,196,246));
        label.setGravity(Gravity.CENTER);
        label.setBackgroundColor(Color.argb(255,255,255,255));
        LayoutParams lp = new LayoutParams(-1,-1);  //创建个布局，填充满整个父局容器
        lp.setMargins(15,15,0,0);
        addView(label,lp);   //然后扔进去
        setNum(0);
    }
    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;

        if (this.num<= 0)
        {
            label.setBackgroundColor(getBackground(num));
            label.setText("");
        }else {
            label.setBackgroundResource(getBackground(num)); //判断数字，然后确定卡片背景颜色
            label.setText(num + "");
        }

    }

    private int getBackground(int num) {
        int bgcolor = defaultBackColor;
        switch (num) {
            case 0:
                bgcolor = Color.argb(70,255,255,255);
                break;
            case 2:
                bgcolor = R.drawable.ddino;
                break;
            case 4:
                bgcolor = R.drawable.dvernon;
                break;
            case 8:
                bgcolor = R.drawable.dboo;
                break;
            case 16:
                bgcolor = R.drawable.ddk;
                break;
            case 32:
                bgcolor = R.drawable.dmingyu;
                break;
            case 64:
                bgcolor = R.drawable.dthe8;
                break;
            case 128:
                bgcolor = R.drawable.dwoozi;
                break;
            case 256:
                bgcolor = R.drawable.dwonwoo;
                break;
            case 512:
                bgcolor = R.drawable.dhoshi;
                break;
            case 1024:
                bgcolor = R.drawable.djun;
                break;
            case 2048:
                bgcolor = R.drawable.djoshua;
                break;
            case 4096:
                bgcolor = R.drawable.djeonghan;
                break;
            case 8192:
                bgcolor = R.drawable.dcoups;
                break;
            default:
                bgcolor = 0xffEA7821;
                break;
        }
        return bgcolor;
    }
    /**
     * 判断卡片的数字是否相同
     * @param
     * @return
     */
    public boolean equals(Card o) {
        return getNum()==o.getNum();
    }

}
